﻿using System.IO;

namespace sync.Library
{
    class sFileMethods
    {
        //Check file is losked
        public virtual bool IsFileLocked(string fileName)
        {
            FileInfo file = new FileInfo(fileName);
            FileStream stream = null;

            try
            {
                stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                //the file is unavailable because it is:
                //still being written to
                //or being processed by another thread
                //or does not exist (has already been processed)
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            //file is not locked
            return false;
        }
        //Write all bytes to file on local path
        public void AppendAllBytes(string path, byte[] bytes)
        {
            //argument-checking here.

            using (var stream = new FileStream(path, FileMode.Append))
            {
                stream.Write(bytes, 0, bytes.Length);
            }
        }
        //Check file if exist in local path
        public void checkFileExist(string fileEx)
        {
            if (!File.Exists(fileEx))
                File.Create(fileEx).Close();
        }
        // filter files not underlist
        public bool filterFile(string fileName)
        {
            if (fileName.Equals("desktop.ini") || fileName.Equals(".dropbox.cache"))
                return false;
            return true;
        }
    }
}
